#ifndef __RGB_POWER_H__
#define __RGB_POWER_H__

#include "base.pb.h"
#include "rgb.pb.h"

int uartRecvData();

struct RgbControl {
    bool fun_one_init = false;
    bool count_flag[8] = {false};
    unsigned int gradual_count[8];
    char cycle_flag;
    char cycle_break;
};

// RGB灯效
enum RgbLampEffect {
    RGB_EFFECT_WORKING_STATE,
    RGB_EFFECT_UNCONNECTED,
    RGB_EFFECT_CONNECTED,
    RGB_EFFECT_TRACKING,
    RGB_EFFECT_PANORAMA,
    RGB_EFFECT_SUCCESS,
    RGB_EFFECT_FAIL,
    RGB_EFFECT_ASTRO_CALIBRATION_SETP1,
    RGB_EFFECT_ASTRO_CALIBRATION_SETP2,
    RGB_EFFECT_ASTRO_CALIBRATION_SETP3,
    RGB_EFFECT_ASTRO_GOTO,
    RGB_EFFECT_ASTRO_TRACKING,
    RGB_EFFECT_ASTRO_STACKING,
    RGB_EFFECT_ASTRO_SUCCESS,
    RGB_EFFECT_ASTRO_FAIL,
    RGB_EFFECT_TL_PHOTOGRAPH,
    RGB_EFFECT_CLOSE,
};

enum RgbState {
    RGB_OFF,
    RGB_ON
};

enum ChargeState {
    CHARGE_NONE,
    CHARGE_SLOW,
    CHARGE_QUICK
};

enum PowerIndState {
    POWER_IND_OFF,
    POWER_IND_ON
};

class RgbPower {
public:
    RgbPower();
    ~RgbPower();

    int handleMessage(WsPacket& ws_packet);
    int initRgbPower();
    int powerOn();
    int powerDown();
    int powerDown(WsPacket& ws_packet);
    int powerIndOn();
    int powerIndOn(WsPacket& ws_packet);
    int powerIndOff();
    int powerIndOff(WsPacket& ws_packet);
    int openRgb();
    int openRgb(WsPacket& ws_packet);
    int closeRgb();
    int closeRgb(WsPacket& ws_packet);
    int setRgbEffect(RgbLampEffect mode);
    int reboot();
    int reboot(WsPacket& ws_packet);

    int getEle();
    RgbState getRgbState();
    ChargeState getChargeState();
    PowerIndState getPowerIndState();
    void waitPressKeyReset();
    bool getPressKeyReset();

private:
    int uartSendData(char *send_buf,int data_len);
    float min(float a, float b, float c);
    float max(float a, float b, float c);
    void hsv2rgb(uint8_t h, uint8_t s, uint8_t v, uint8_t *G, uint8_t *R, uint8_t *B);
    uint8_t gaussian2(uint32_t count, uint8_t value, uint8_t flag, int countNum);
    int sendCustomData(uint8_t *buf);
    void gradual_count(unsigned char *value, int num);
    void gradual4(uint8_t *buf, int countNum);
    void gradual6(uint8_t *buf, int count_num);
    int custom_mode_2(uint8_t h, uint8_t s, uint8_t v, int check_hsv, int delay, bool cycle_flag, int cycle_num);
    int custom_mode_4(uint8_t h, uint8_t s, uint8_t v, int check_hsv, int delay, bool cycle_flag, int cycle_num);
    int setEffect(RgbLampEffect mode);
    int unconnected();
    int connected();
    int tracking(); 
    int panorama();
    int success();
    int fail();
    int astroCalibrationStep1();
    int astroCalibrationStep2();
    int astroCalibrationStep3();
    int astroGoto();
    int astroTracking();
    int astroStacking();
    int astroSuccess();
    int astroFail();
    int tlPhotograph();
    int rgbClear();
    int rgbControl(int mode, int hue, int saturation, int value, int background, int delay_time, int breathe_delay, int cycleFlag, int cycleNum);
    int uartRecvData();
    void parseUartData(char *buf, int len);
    int checkEle();
    int disableReverseCharging();

    void setRgbState(RgbState state);
    void setChargeState(ChargeState state);
};




#endif